package com.volcreate.exception_collection.no11;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @author zxc
 * @date 2020/10/5
 */

public class Main {


    static final String[] KEYWORDS = {
            "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class",
            "const", "continue", "default", "do", "double", "else", "enum", "extends", "false", "final", "finally",
            "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
            "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super",
            "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"
    };

    public static HashMap<String, Integer> map = new HashMap<>();

    public static void main(String[] args) throws Exception {

        for (String keyword : KEYWORDS) {
            map.put(keyword, 0);
        }
        StringBuilder builder = new StringBuilder();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String line = scanner.nextLine();
            if ("exit".equals(line)) break;
            builder.append(analysisLine(line));
        }

        if ("".equals(builder.toString())) {
            System.out.println("Wrong Format");
        } else {
            matchKeywords(matchLine(builder.toString()));
            map.keySet().stream().filter(ss -> map.get(ss) > 0).sorted().forEach(k -> {
                        System.out.println(map.get(k) + "\t" + k);
                    }
            );
//            Object[] keys = map.keySet().stream().sorted().filter(ss -> map.get(ss) > 0).toArray();
//            for (int i = 0; i < keys.length; i++) {
//                System.out.print(map.get(keys[i])+"\t"+keys[i]);
//                if (i!=keys.length-1) System.out.println();
//            }
        }

    }

    public static String analysisLine(String line) {
        if (line.contains("//")) {
            return line.substring(0, line.indexOf("//")) + " ";
        }
        return line + " ";
    }

    public static String matchLine(String line) {
        String s = line.replaceAll("\".*?\"", "")
                .replaceAll("/\\*\\*.*?\\*/", "")
                .replaceAll("/\\*.*?\\*/", "");
        return s;
    }

    public static void matchKeywords(String line) {
        String[] wordList = line.replaceAll("\\W", " ").split(" ");
        for (int i = 0; i < wordList.length; i++) {
            for (int j = 0; j < 53; j++) {
                if (wordList[i].equals(KEYWORDS[j])) { //循环判断
                    map.put(KEYWORDS[j], map.get(KEYWORDS[j]) + 1);
                }
            }
        }
    }

}



